/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.data.manager;

import cz.cvut.fel.taskpro.data.entity.AssignmentSubmit;
import cz.cvut.fel.taskpro.data.entity.Version;
import cz.cvut.fel.taskpro.data.entity.VersionFile;
import cz.cvut.fel.taskpro.data.imanager.IVersionData;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;


/**
 * This class implement #{@link IVersionData} interface
 * @author Tom
 * @version 1.0. beta
 */
@Stateless
public class VersionData extends ObjectManager implements Serializable, IVersionData{

    @Override
    public List<VersionFile> getAllVersionFileByAssignmentSubmit(AssignmentSubmit assignmentSubmit) {       
        CriteriaBuilder cb=em.getCriteriaBuilder();
        CriteriaQuery<VersionFile> versionFileQuery=cb.createQuery(VersionFile.class);
        Root<VersionFile> versionFileRoot=versionFileQuery.from(VersionFile.class);
        Subquery versionQuery=versionFileQuery.subquery(Version.class);  
        Join<Version,VersionFile> versions=versionFileRoot.join("version");
        Predicate versionPredicate=cb.equal(versions.get("assignmentSubmit"), assignmentSubmit);
        versionQuery.where(versionPredicate); 
        versionFileQuery.where(cb.exists(versionQuery));
        TypedQuery typedQuery=em.createQuery(versionFileQuery);
        List<VersionFile> resultList=typedQuery.getResultList();
        return resultList;
    }

    @Override
    public void updateVersionFile(VersionFile versionFileToUpdate) {
        super.update(versionFileToUpdate);
    }
    
}
